<template>
	<div id="paramInfo">
		<el-row style="margin-bottom: 20px;">
			<el-col :span="12" style="line-height:33px;">组织架构配置</el-col>
			<el-col :span="12" style="text-align: right">
				<el-button
					size="small"
					type="primary"
					icon="el-icon-plus"
					@click="openDialog"
					>新增</el-button
				>
			</el-col>
		</el-row>
		<Table
			:tableData="tableData"
			:tableHead="tableHead"
			:isUseSlot="false"
			:page="listQuery.page"
			:limit="listQuery.limit"
			v-loading="listLoading"
		>
			<template slot="tool" slot-scope="{ data }">
				<el-button
					style="padding:3px;"
					icon="el-icon-document"
					@click="handleEdit(data.row)"
					type="text"
					>编辑</el-button
				>
				<el-button
					style="color:red;padding:3px;"
					icon="el-icon-delete"
					type="text"
					@click="delet(data.row)"
					>删除</el-button
				>
			</template>
		</Table>
		<Pagination
			v-show="total > 0"
			:total="total"
			:page.sync="listQuery.page"
			:limit.sync="listQuery.limit"
			@pagination="getList"
		></Pagination>
		<form-tem
			:info="{ title: '组织架构配置', width: '600px' }"
			:formList="formList"
			@submit="submit"
			@close="closed"
			ref="formDelg"
		></form-tem>
	</div>
</template>

<script>
import Table from "@/components/Table";
import Pagination from "@/components/Pagination"; // 分页
import { getOrganizationList, getOrganizationInfo, insertOrganizationInfo, updateOrganizationInfo } from "@/api/RCIM/RCIM-systemConfig";
import { getDepartment } from "@/api/RCIM/RCIM-select";
import { deepRecursiveGetStringIdLine } from "@/utils";
import VForm from "@/components/luyiming/VueEleForm/index.vue";
import formTem from "@/template/form.temp";
import { validPhone } from "@/utils/validate";
import { deleteOrganizationInfo } from '@/api/RCIM/RCIM-delet';
export default {
	components: {
		Table,
		Pagination,
		VForm,
		formTem
	},
	data() {
		return {
			tableData: [
				{
					departmentName: "xxxxx",
					higherDepartment: "xxxxx"
				}
			],
			tableHead: [
				{
					prop: "departmentName",
					label: "部门名称",
					width: "",
					align: "center"
				},
				{
					prop: "higherDepartment",
					label: "上级部门",
					width: "",
					align: "center"
				},
				{
					prop: "operate",
					label: "操作",
					width: "",
					align: "center"
				}
			],
			listLoading: false,
			total: 0,
			listQuery: {
				page: 1,
				limit: 10
			},
			dialogFormVisible: false,
			formList: [
				{
					list: [
						{
							type: "input",
							width: "60%",
							label: "部门名称",
							placeholder: "请输入",
							moduleKey: "departmentName",
							showWordLimit: true,
							maxlength: '20',
							validate: function() {
								return [
									{
										required: true,
										message: "请输入部门名称",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "cascader",
							props: {
								checkStrictly: true,
							},
							label: "上级部门",
							width: "60%",
							placeholder: "请输入",
							moduleKey: "higherDepartment",
							fetchTimes: Infinity,
							fetch: getDepartment,
							options: [],
						}
					],
					// title: "部门信息"
				}
			]
		};
	},
	created() {
		this.getList()
	},
	methods: {
		/**
		 * @desc 删除数据
		 * @param {Object} row 表格行数据
		 */
		delet(row){
			this.$confirm('确认要删除吗？')
				.then(_ => {
					deleteOrganizationInfo(row.id).then(res => {
						if(res.code === 200){
							this.getList()
							return this.$message({
								type: "success",
								message: "删除成功"
							})
						}
						return this.$message({
							type: "error",
							message: "删除失败"
						})
					})
				})
				.catch(_ => {});
		},
		/**
		 * @desc 获取表格数据
		 */
		async getList() {
			const {data} = await getOrganizationList(this.listQuery)
			console.log(data);
			this.tableData = data.organizationList
			this.total = data.total
		},
		/**
		 * @desc 编辑表格数据
		 * @param {Object} row 表格行数据
		 */
		handleEdit(row) {
			this.openDialog();
			getOrganizationInfo({id: row.id}).then(
				res => {
					this.$refs.formDelg.getFormEle().forEach(item => {
						item[0].getFormList("higherDepartment").then(resTree => {
							const dataValue = res.data.data;
							dataValue.higherDepartment = deepRecursiveGetStringIdLine(
								resTree,
								dataValue.higherDepartment,
								"value"
							);
							this.$nextTick(() => {
								item[0].setDefaultValue(dataValue);
							});
						});
					});
				}
			)

		},
		/**
		 * @desc 新增、编辑数据
		 * @param {Object} formData 表单数据
		 */
		submit(formData) {
			console.log(formData);
			formData.higherDepartment = formData.higherDepartment.pop?.() || '';
			if( formData.id ){
				return updateOrganizationInfo(formData).then(res => {
					if (res.code === 200) {
						this.getList()
						this.closeDialog();
						return this.$message({
							type: "success",
							message: "编辑成功"
						});
					}
					return this.$message({
						type: "error",
						message: "编辑失败"
					});
				})
			}
			// 调用新增接口
			insertOrganizationInfo(formData).then(res => {
					if (res.code === 200) {
						this.getList();
						this.closeDialog();
						return this.$message({
							type: "success",
							message: "添加成功"
						});
					}
					return this.$message({
						type: "error",
						message: "添加失败"
					});
				})
		},
		/**
		 * @desc 关闭弹窗
		 */
		closeDialog() {
			this.$refs.formDelg.closeDialog();
		},
		/**
		 * @desc 关闭弹窗回调
		 */
		closed(){
			this.$refs.formDelg.getFormEle().forEach(item => {
				item[0].deleteKeys(["id"])
			});
		},
		/**
		 * @desc 打开弹窗
		 */
		openDialog() {
			this.$refs.formDelg.openDialog();
		}
	}
};
</script>

<style lang="scss" scoped>
#paramInfo {
	height: calc(100vh - 144px);
	overflow: hidden;
	padding: 30px 50px;
}
</style>
